<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.therapy_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10">
      <el-col :span="6" v-for="(item, index) in treatmentList" :key="item.id" style="margin-bottom: 20px;">
        <el-card :body-style="{ padding: '20px', height: '380px', display: 'flex', flexDirection: 'column', justifyContent: 'space-between' }">
          <div style="display: flex; align-items: center; margin-bottom: 10px;">
            <!-- 理疗图片 -->
            <image-preview :src="item.image" :width="120" :height="120" style="margin-right: 20px;"/>
            <div>
              <!-- 名称 -->
              <h3>{{ item.name }}</h3>
              <!-- 项目分类 -->
              <p>{{ item.categoryName }}</p>
              <!-- 时长 -->
              <p>{{ item.duration }}</p>
            </div>
          </div>
          <!-- 其他 -->
          <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-top: -10px;">
            <div>
              <p><strong>价格：</strong>{{ item.price }}</p>
              <p><strong>描述：</strong>{{ item.description }}</p>
              <p><strong>功效：</strong>{{ item.effect }}</p>
            </div>
            <div>
              <p><dict-tag :options="dict.type.therapy_status" :value="item.status"/></p>
            </div>
          </div>
          <!-- 操作按钮 -->
          <div style="margin-top: auto;">
            <el-button size="mini" type="text" @click="handleUpdate(item)">查看详细</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 展示理疗项目信息的对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-row>
        <el-col :span="8">
          <image-preview :src="form.image" :width="100" :height="100"/>
        </el-col>
        <el-col :span="16">
          <div>
            <p><strong>名称：</strong>{{ form.name }}</p>
            <p><strong>类型：</strong>{{ form.categoryName }}</p>
            <p><strong>时长：</strong>{{ form.duration }}</p>
          </div>
        </el-col>
      </el-row>
      <div style="display: flex; justify-content: space-between;">
        <div>
          <p><strong>价格：</strong>{{ form.price }}</p>
          <p><strong>描述：</strong>{{ form.description }}</p>
          <p><strong>功效：</strong>{{ form.effect }}</p>
        </div>
        <div>
          <dict-tag :options="dict.type.therapy_status" :value="form.status"/>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">下 单</el-button>
        <el-button  @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTreatment, getTreatment, delTreatment, addTreatment, updateTreatment } from "@/api/treatment/treatment";
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/orders/orders";

export default {
  name: "Treatment",
  dicts: ['therapy_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 理疗项目表格数据
      treatmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        categoryId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "项目分类不能为空", trigger: "change" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        duration: [
          { required: true, message: "时长不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询理疗项目列表 */
    getList() {
      this.loading = true;
      listTreatment(this.queryParams).then(response => {
        this.treatmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        categoryId: null,
        price: null,
        image: null,
        effect: null,
        duration: null,
        description: null,
        status: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null,
        amount: null,
        taboos: null,
        suitable: null,
        goodsId: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加理疗项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTreatment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "项目详细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.id != null) {
        this.form.goodsId = this.form.id;
        this.form.status = 10;
        // this.form.userId = 107;
        addOrders(this.form).then(response => {
          this.$modal.msgSuccess("下单成功，请在理疗订单中支付~");
          this.open = false;
          this.getList();
        });
      }
    },
  }
};
</script>


<style scoped>
.custom-card {
  height: 400px; /* 设置卡片的固定高度 */
}
</style>
